<?php

namespace App\Http\Resources\Stats\User\Player;

use App\Http\Resources\Resource;
use Helper\Timed;

class WeekResource extends Resource
{

	/**
	 * Transform the resource into an array.
	 *
	 * @param \Illuminate\Http\Request $request
	 * @return array
	 */
	public function toArray($request)
	{
		$charts = [
			'timed' => [],
			'data' => [
				'u_game' => [],
				'u_sign' => [],
				'nu_game' => [],
				'nu_sign' => [],
			],
		];

		$datetime = $request->input('datetime');

		$datetime = strtotime(date('Y-01-01', $datetime));

		list($timed, $timedEnd) = Timed::datetimeFormat($request,'datetime', $datetime, strtotime('+362 days', $datetime));

		$stats = $this->resource['stats'];

		date_default_timezone_set(Timed::getTimezoneName());
		for (; $timed < $timedEnd; $timed += 86400 * 7) {

			$y = date('Y', $timed);
			if ($y < date('Y') || $y > date('Y')) {
				continue;
			}

			$w = date('W', $timed);

			$charts['timed'][] = $w;
			$nuGame = $w = (int)$w;
			$nuGame -= 1;
			$charts['data']['u_game'][] = $stats['u_game'][$w] ?? null;
			$charts['data']['u_sign'][] = $stats['u_sign'][$w] ?? null;
			$charts['data']['nu_game'][] = $stats['nu_game'][$nuGame] ?? null;
			$charts['data']['nu_sign'][] = $stats['nu_sign'][$nuGame] ?? null;
		}

		return $this->getResponse()->setResponseData($charts);
	}


    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    // public function toArray($request)
    // {
    //     $charts = [
    //         'timed' => [],
    //         'data' => [
    //             'u_game' => [],
    //             'u_sign' => [],
    //             'nu_game' => [],
    //             'nu_sign' => [],
    //         ],
    //     ];
	//
	//     $datetime = $request->input('datetime');
	//
	//     $datetime = strtotime(date('Y-01-01', $datetime));
	//
	//     list($timed, $timedEnd) = Timed::datetimeFormat($request,'datetime', $datetime, strtotime('+1 year', $datetime));
	//
    //     $stats = $this->resource['stats'];
	//
    //     for (; $timed < $timedEnd; $timed += 86400 * 7) {
	//         $date = date('W', $timed);
    //         $charts['timed'][] = date('W', $timed);
    //         // $charts['data']['u_game'][] = $stats[$timed]['u_game'] ?? null;
    //         // $charts['data']['u_sign'][] = $stats[$timed]['u_sign'] ?? null;
    //         // $charts['data']['nu_game'][] = $stats[$timed]['nu_game'] ?? null;
    //         // $charts['data']['nu_sign'][] = $stats[$timed]['nu_sign'] ?? null;
	//         $charts['data']['u_game'][] = $stats['u_game'][$date]['total'] ?? null;
	//         $charts['data']['u_sign'][] = $stats['u_sign'][$date]['total'] ?? null;
	//         $charts['data']['nu_game'][] = $stats['nu_game'][$date]['total'] ?? null;
	//         $charts['data']['nu_sign'][] = $stats['nu_sign'][$date]['total']  ?? null;
    //     }
	//
    //     return $this->getResponse()->setResponseData($charts);
	//
    // }
}
